//
//  JiangDaoListViewController.m
//  JiuKuGospel
//
//  Created by jiukuyinyue on 16/8/3.
//  Copyright (c) 2016年 JiuKuForApp. All rights reserved.
//

#import "JiangDaoListViewController.h"
#import "JiangdaoListTableViewCell.h"
#import "JKMVWebViewController.h"
#define HEADER_HEIGHT 180

@interface JiangDaoListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)NSMutableArray * listArr;
@property (nonatomic, strong)UITableView * songsListTab;
@property (nonatomic, strong)UIImageView * headerImageView;
@property (nonatomic, strong)UILabel * titleLabel;
@property (nonatomic, strong)UILabel * jianjieLabel;
@property (nonatomic, strong)FXBlurView *blurView;

@property (nonatomic, assign)NSInteger pageNum;


@end

@implementation JiangDaoListViewController

- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:YES];
    
    self.navigationController.navigationBar.hidden = YES;
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.playSongView.hidden = YES;

    
    //百度开始页面统计
    [[BaiduMobStat defaultStat] pageviewStartWithName:@"songsListPage"];
    
    [_songsListTab reloadData];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    //百度结束页面统计
    [[BaiduMobStat defaultStat] pageviewEndWithName:@"songsListPage"];
    
}

- (BOOL)canBecomeFirstResponder{
    return YES;
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    [[TheHomeManager shareTheHomeManager].playVC remoteControlReceivedWithEvent:event];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.hidden = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    //导航栏颜色
    self.navigationController.navigationBar.barTintColor = JK_HUISE_LINE_COLOR;
    //导航栏字体颜色
    self.navigationItem.title = @"";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    UIImageView * LeftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, 11, 19)];
    LeftImageView.userInteractionEnabled = YES;
    NSString * backStr = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"new返回%@", [TheHomeManager shareTheHomeManager].iPhone23] ofType:@"png"];
    LeftImageView.image = [UIImage imageWithContentsOfFile:backStr];
    [leftView addSubview:LeftImageView];
    UITapGestureRecognizer * backTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftBtnAction)];
    [leftView addGestureRecognizer:backTap];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftView];
    
    
    _pageNum = 1;
    [self setupUI];
    [self loadJiangDaoData];
    
}

- (void)leftBtnAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setupUI
{
    self.songsListTab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-0) style:UITableViewStylePlain];
    self.songsListTab.showsVerticalScrollIndicator = NO;
    //区头顶头
    self.automaticallyAdjustsScrollViewInsets = NO;
    //    _songsListTab.backgroundColor = JK_CLEAR_COLOR;
    _songsListTab.delegate = self;
    _songsListTab.dataSource = self;
    [self.view addSubview:_songsListTab];
    
    __block JiangDaoListViewController * listVC = self;
    [_songsListTab addHeaderWithCallback:^{
        listVC.pageNum = 1;
        [listVC loadJiangDaoData];
        
    }];
    
    [_songsListTab addFooterWithCallback:^{
        listVC.pageNum++;
        [listVC loadJiangDaoData];
    }];
    
    
    //cell分割线顶头
    if ([self.songsListTab respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.songsListTab setSeparatorInset: UIEdgeInsetsZero];
    }
    if ([self.songsListTab respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.songsListTab setLayoutMargins: UIEdgeInsetsZero];
    }

    UIScrollView * headerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, HEADER_HEIGHT)];
    headerView.scrollsToTop = NO;

}

- (void)loadJiangDaoData
{
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode=MBProgressHUDAnimationFade;//枚举类型不同的效果

    NSDictionary * dic = @{@"type":@"jiangdao", @"parentId":self.listID, @"page":[NSString stringWithFormat:@"%ld", _pageNum], @"size":@"30"};
    [HttpTool postWithURL:kBasePostFuyin params:dic success:^(id json) {
        [self hiddenMBAction];
        if ([[json valueForKey:@"data"] isKindOfClass:[NSArray class]]) {
            
            [_headerImageView sd_setImageWithURL:[NSURL URLWithString:self.headerImageUrl] placeholderImage:[UIImage imageNamed:@"450x450@3x.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                [self changeBlurView];
            }];

            if (_pageNum == 1) {
                [self.listArr removeAllObjects];
            }
            NSArray * arr = [json objectForKey:@"data"];
            [self.listArr addObjectsFromArray:arr];
            [_songsListTab headerEndRefreshing];
            [_songsListTab footerEndRefreshing];
            [_songsListTab reloadData];
        }
        
    } failure:^(NSError *error) {
        [_songsListTab headerEndRefreshing];
        [_songsListTab footerEndRefreshing];
        [self hiddenMBAction];

    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)changeBlurView
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangBlur" object:nil];
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)hiddenMBAction
{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}

#pragma mark - listTable代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%ld", _listArr.count);
    return _listArr.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return HEADER_HEIGHT;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * newCellStr = @"SongNewCell";
    JiangdaoListTableViewCell * listCell = [tableView dequeueReusableCellWithIdentifier:newCellStr];
    if (!listCell) {
        listCell = [[JiangdaoListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:newCellStr];
        listCell.width = self.view.width;
        listCell.height = 60;
    }

    listCell.status = [_listArr objectAtIndex:indexPath.row];
    listCell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row+1];

    return listCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary * statusDic = [self.listArr objectAtIndex:indexPath.row];
    
    JKMVWebViewController * webView = [[JKMVWebViewController alloc] init];
    webView.webStr = [statusDic objectForKey:@"playurl"];
    webView.webTitle = [statusDic objectForKey:@"title"];

    [self.navigationController pushViewController:webView animated:YES];

}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, HEADER_HEIGHT)];
    headerView.clipsToBounds = YES;
    if ([self.titleStr isEqualToString:@"诗歌"]) {
        headerView.frame = CGRectMake(0, 0, self.view.width, HEADER_HEIGHT+40);
    }
    self.headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, HEADER_HEIGHT)];
    _headerImageView.userInteractionEnabled = YES;
    self.headerImageView.contentMode = UIViewContentModeScaleAspectFill;
    
    //    _songsListTab.tableHeaderView = _headerImageView;
    [headerView addSubview:_headerImageView];
    
    self.blurView = [[FXBlurView alloc] initWithFrame:CGRectMake(0, 0, _headerImageView.width, _headerImageView.height)];
    // 背景毛玻璃效果
    [self.blurView setTintColor:[UIColor clearColor]];
    [self.blurView setBlurEnabled:YES];
    [self.blurView setBlurRadius:10.0f];
    [headerView addSubview:_blurView];
    
    GFMaskView *darkView = [[GFMaskView alloc] initWithFrame:CGRectMake(0, 0, _headerImageView.width, _headerImageView.height)];
    darkView.backgroundColor = [UIColor blackColor];
    darkView.alpha = 0.2f;
    [self.blurView addSubview:darkView];
    
    
    
    //标题title
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, _headerImageView.width, 44)];
    _titleLabel.userInteractionEnabled = YES;
    _titleLabel.text = self.titleStr;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = [UIColor whiteColor];
    [_titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
    [_blurView addSubview:_titleLabel];
    
    UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    UIImageView * LeftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 12, 11, 19)];
    LeftImageView.userInteractionEnabled = YES;
    LeftImageView.image = [UIImage imageNamed:@"header_back2@3x.png"];
    [leftView addSubview:LeftImageView];
    UITapGestureRecognizer * backTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backAction)];
    [leftView addGestureRecognizer:backTap];
    
    [_titleLabel addSubview:leftView];
    
    
    //简介
//    self.jianjieLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, _headerImageView.height-80, self.view.width-2*20, 60)];
//    
//    _jianjieLabel.textColor = [UIColor whiteColor];
//    _jianjieLabel.numberOfLines = 0;
//    _jianjieLabel.font = FONT_14;
//    [_blurView addSubview:_jianjieLabel];
    
    
    
    
    
    //黑屏BUG
        [_headerImageView sd_setImageWithURL:[NSURL URLWithString:self.headerImageUrl] placeholderImage:[UIImage imageNamed:@"450x450@3x.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [self changeBlurView];
        }];
//        _listNameLabel.text = @"";
//        NSString * contentStr = [@"" stringByReplacingOccurrencesOfString:@"<br/>" withString:@""];
//        _jianjieLabel.text = self.jianjieStr;
//        NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:_jianjieLabel.text];
//        NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//        /** 行间距  */
//        CGFloat space = 4.0;
//        [paragraphStyle setLineSpacing:space];
//        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [_jianjieLabel.text length])];
//        /** 字间距 */
//        long number = 0.0f;
//        CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
//        [attributedString addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedString length])];
//        CFRelease(num);
//        [_jianjieLabel setAttributedText:attributedString];
//        _jianjieLabel.lineBreakMode =NSLineBreakByTruncatingTail;
//        _titleLabel.text = self.headerTitle;
    _titleLabel.text = self.titleStr;
    
    return headerView;
}



- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)listArr
{
    if (_listArr == nil) {
        self.listArr = [NSMutableArray array];
    }
    return _listArr;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
